home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / imc9103.zip / ACO.C next >
Text File  |  1991-02-12  |  9KB  |  240 lines

  1. /**************************************************************
  2. * ACO.C - this program calculates the execution time of the   *
  3. *   ACO -- the average C operator.                            *
  4. *                                                             *
  5. * To compile: "cl /Od /Fa /Gs aco.c"                          *
  6. * RHS 9/30/90                                                                  *
  7. **************************************************************/
  8. #include<time.h>
  9. #include<process.h>
  10. #include<stdio.h>
  11. #include<stdlib.h>
  12.  
  13. #define DEF_ITERATIONS  10000000L
  14. #define TRUE 1
  15. #define FALSE 0
  16. #define MAXOPS  50
  17.  
  18. typedef struct _op {
  19.     char *name;
  20.     void (*fptr)();
  21.     float elapsed;
  22.     } Op;
  23.  
  24. void cdecl main(void);
  25. int  cdecl time_ops(Op*, float*);
  26.  
  27. /**************************************************************
  28. * global variable declarations                                *
  29. **************************************************************/
  30. struct _dummy
  31.     {
  32.     int i;
  33.     float f;
  34.     double d;
  35.     } dummy;
  36.  
  37. struct _dummy *dum = &dummy;
  38.  
  39. int    nArray[10];
  40. float  fArray[10];
  41. int    i1, i2 = 3, i3 = 7, i4, *ip1, *ip2;
  42. float  f1, f2=3.0, f3=7.0, f4, *fp1;
  43. double d1, d2;
  44. long   l1, l2;
  45. char   c1;
  46.  
  47. /**************************************************************
  48. * Non-floating point operations -- test functions             *
  49. **************************************************************/
  50. void nEmptyfunc(void) { i1 = i2;   }
  51. void n00(void) { i1 = i4 = i3;     }
  52. void n01(void) { i1 = dummy.i;     }
  53. void n02(void) { i1 = dum->i;      }
  54. void n03(void) { i1 = nArray[i2];  }
  55. void n04(void) { i1 = *ip1;        }
  56. void n05(void) { i1 = 10;          }
  57. void n06(void) { i1 = (long)i2;    }
  58. void n07(void) { i1 = (char)i2;    }
  59. void n08(void) { i1 = (int)l2;     }
  60. void n09(void) { i1 = ~i2;         }
  61. void n10(void) { i1 = ++i2;        }
  62. void n11(void) { i1 = i2++;        }
  63. void n12(void) { i1 = !i2;         }
  64. void n13(void) { i1 = -i2;         }
  65. void n14(void) { i1 = i2 * i3;     }
  66. void n15(void) { i1 = i2 / i3;     }
  67. void n16(void) { i1 = i2 % i3;     }
  68. void n17(void) { i1 = i2 + i3;     }
  69. void n18(void) { i1 = i2 - i3;     }
  70. void n19(void) { i1 = i2 << i3;    }
  71. void n20(void) { i1 = i2 >> i3;    }
  72. void n21(void) { i1 = i2 <= i3;    }
  73. void n22(void) { i1 = i2 != i3;    }
  74. void n23(void) { i1 = i2 && i3;    }
  75. void n24(void) { i1 = i2 || i3;    }
  76. void n25(void) { i1 = i2 & i3;     }
  77. void n26(void) { i1 = i2 | i3;     }
  78. void n27(void) { i1 = i2 ^ i3;     }
  79. void n28(void) { i1 = i1 ? i2 : i3;}
  80. void n29(void) { i1 += i2;         }
  81.  
  82. Op nOperations[MAXOPS] =
  83.     {
  84.     { "empty function..............", nEmptyfunc, (float)0},
  85.     { "integer assignment..........", n00, (float)0},
  86.     { "member reference............", n01, (float)0},
  87.     { "member reference via pointer", n02, (float)0},
  88.     { "array reference.............", n03, (float)0},
  89.     { "pointer reference...........", n04, (float)0},
  90.     { "constant reference..........", n05, (float)0},
  91.     { "cast long to int............", n06, (float)0},
  92.     { "cast int to char............", n07, (float)0},
  93.     { "cast long to int............", n08, (float)0},
  94.     { "compliment..................", n09, (float)0},
  95.     { "pre-increment...............", n10, (float)0},
  96.     { "post-increment..............", n11, (float)0},
  97.     { "not.........................", n12, (float)0},
  98.     { "negative....................", n13, (float)0},
  99.     { "multiply....................", n14, (float)0},
  100.     { "divide......................", n15, (float)0},
  101.     { "modulo......................", n16, (float)0},
  102.     { "addition....................", n17, (float)0},
  103.     { "subtraction.................", n18, (float)0},
  104.     { "shift-left..................", n19, (float)0},
  105.     { "shift-right.................", n20, (float)0},
  106.     { "integer compare.............", n21, (float)0},
  107.     { "integer equality............", n22, (float)0},
  108.     { "logical and.................", n23, (float)0},
  109.     { "logical or..................", n24, (float)0},
  110.     { "bit-wise and................", n25, (float)0},
  111.     { "bit-wise or.................", n26, (float)0},
  112.     { "exclusive or................", n27, (float)0},
  113.     { "conditional.................", n28, (float)0},
  114.     { "plus/equals.................", n29, (float)0},
  115.     { "", NULL, (float)0 }
  116.     };
  117.  
  118. /**************************************************************
  119. * Floating point operations -- test functions                 *
  120. **************************************************************/
  121. void fEmptyfunc(void) { f1 = f2;   }
  122. void f00(void) { f1 = f4 = f2;     }
  123. void f01(void) { f1 = dummy.f;     }
  124. void f02(void) { f1 = dum->f;      }
  125. void f03(void) { f1 = fArray[i1];  }
  126. void f04(void) { f1 = *fp1;        }
  127. void f05(void) { f1 = 10.0;        }
  128. void f06(void) { f1 = (float)c1;   }
  129. void f07(void) { f1 = (float)i1;   }
  130. void f08(void) { f1 = (float)l1;   }
  131. void f09(void) { f1 = ++f2;        }
  132. void f10(void) { f1 = f1++;        }
  133. void f11(void) { f1 = !f2;         }
  134. void f12(void) { f1 = -f2;         }
  135. void f13(void) { f1 = f2 * f3;     }
  136. void f14(void) { f1 = f2 / f3;     }
  137. void f15(void) { f1 = f2 + f3;     }
  138. void f16(void) { f1 = f2 - f3;     }
  139. void f17(void) { f1 = f2 <= f3;    }
  140. void f18(void) { f1 = f2 != f3;    }
  141.  
  142. Op fOperations[MAXOPS] =
  143.     {
  144.     { "empty function..............", fEmptyfunc, (float)0},
  145.     { "floating point assignment...", f00, (float)0},
  146.     { "member reference............", f01, (float)0},
  147.     { "member reference via pointer", f02, (float)0},
  148.     { "array reference.............", f03, (float)0},
  149.     { "pointer reference...........", f04, (float)0},
  150.     { "constant reference..........", f05, (float)0},
  151.     { "cast to fp from char........", f06, (float)0},
  152.     { "cast to fp from int.........", f07, (float)0},
  153.     { "cast to fp from long........", f08, (float)0},
  154.     { "pre-increment...............", f09, (float)0},
  155.     { "post-increment..............", f10, (float)0},
  156.     { "not.........................", f11, (float)0},
  157.     { "negative....................", f12, (float)0},
  158.     { "multiply....................", f13, (float)0},
  159.     { "divide......................", f14, (float)0},
  160.     { "addition....................", f15, (float)0},
  161.     { "subtraction.................", f16, (float)0},
  162.     { "compare.....................", f17, (float)0},
  163.     { "equality....................", f18, (float)0},
  164.     { "", NULL, (float)0 }
  165.     };
  166.  
  167. /**************************************************************
  168. * time_ops - time each operation in table Operations[]        *
  169. * through successive iterations. The number of iterations     *
  170. * is specified in the global variable "settings".             *
  171. *                                                             *
  172. * INP: Operations - the NULL terminated table containing      *
  173. *                   operations to be performed.               *
  174. *      ACOval     - holds the ACO value upon return.          *
  175. * OUT: the number of operations tested.                       *
  176. **************************************************************/
  177. int time_ops(Op *Operations, float *ACOval)
  178.     {
  179.     long start, end;
  180.     unsigned long iterations, setting = DEF_ITERATIONS;
  181.     float test,empty,total = (float)0;
  182.     int i;
  183.  
  184.     for( i = 0; Operations[i].name[0] ; i++)
  185.         {
  186.         printf("Testing %s...",Operations[i].name);
  187.         iterations = setting;
  188.         start = clock();
  189.         for( ; iterations; iterations--)
  190.             Operations[i].fptr();
  191.         end = clock();
  192.         test = (float)(end-start);
  193.         test /= CLK_TCK;
  194.  
  195.         if(i == 0)      // testing empty function on i == 0
  196.             Operations[i].elapsed = empty = test;
  197.         else            // all other functions on i > 0
  198.             {
  199.             if(test < empty)
  200.                 test = empty;
  201.             Operations[i].elapsed = test - empty;
  202.             total += Operations[i].elapsed;
  203.             }
  204.  
  205.         printf("%04.02f seconds, net %04.02f\n",
  206.                  test, Operations[i].elapsed);
  207.         }
  208.  
  209.     printf("Empty function required: %04.02f\n",empty);
  210.     printf("%d operations netted %04.02f seconds"
  211.            " over %ld iterations\n", i, total, setting);
  212.     printf("An average of %04.02f seconds per operations"
  213.            "  for %ld iterations\n", total=total/i, setting);
  214.     *ACOval = 1E6 * total/setting;
  215.     return(i);
  216.     }
  217.  
  218. /**************************************************************
  219. * main - calculates ACO, ACON and ACOF based on a subset of   *
  220. * C operations.                                               *
  221. **************************************************************/
  222. void main(void)
  223.     {
  224.     int   n1, n2;
  225.     float ACO, ACON, ACOF;
  226.  
  227.     printf("\n=====Non-Floating Point Operations=====\n");
  228.     n1 = time_ops(nOperations, &ACON);
  229.     printf("ACON = %04.2f microseconds\n", ACON);
  230.  
  231.     printf("\n=====Floating Point Operations=====\n");
  232.     n2 = time_ops(fOperations, &ACOF);
  233.     printf("ACOF = %04.2f microseconds\n", ACOF);
  234.  
  235.     printf("\n=====Combined Operations=====\n");
  236.     ACO = (ACON*n1 + ACOF*n2) / (float)(n1+n2);
  237.     printf("ACO = %04.2f microseconds\n", ACO);
  238.     }
  239.  
  240.